package com.schoolcloud.social.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.schoolcloud.common.model.PageDTO;
import com.schoolcloud.social.entity.SocPost;
import com.schoolcloud.social.mapper.SocPostMapper;
import com.schoolcloud.social.query.PostQuery;
import com.schoolcloud.social.service.*;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author hongmizfb
 * @description 针对表【soc_post】的数据库操作Service实现
 * @createDate 2025-02-10 16:17:19
 */
@Service
@RequiredArgsConstructor
public class SocPostServiceImpl extends ServiceImpl<SocPostMapper, SocPost> implements SocPostService {


    @Override
    public void modifyPost(SocPost socPost) {
        socPost.setUpdateTime(new Date());
        updateById(socPost);
    }

    @Override
    public Page<SocPost> getMpPostPage(PostQuery query) {
        String keywords = query.getKeywords();
        String bardId = query.getBardId();
        int range = query.getRange();
        int status = query.getStatus();
        return lambdaQuery().eq(StrUtil.isNotBlank(keywords), SocPost::getTitle, keywords)
                .eq(SocPost::getPostRange, range)
                .eq(StrUtil.isNotBlank(bardId), SocPost::getBardId, bardId)
                .eq(status != -1, SocPost::getStatus, status)
                .page(query.toMpPage());
    }

    @Override
    public PageDTO<SocPost> getPostPage(PostQuery query) {
        PageDTO<SocPost> dto = new PageDTO<>();
        List<SocPost> postPage = baseMapper.getPostPage(query);
        dto.setList(postPage);
        dto.setPageNum(query.getPageNum());
        dto.setTotal(baseMapper.getPostCount(query));
        return dto;
    }

    @Override
    public Long getUserPostCount(String userId) {
        return lambdaQuery().eq(SocPost::getUserId, userId).count();
    }

    @Override
    public Long getPostCountByBardId(String bardId) {
        return lambdaQuery().eq(SocPost::getBardId, bardId).count();
    }

    @Override
    public List<SocPost> getUserPostList(String userId) {
        return lambdaQuery().eq(SocPost::getUserId, userId).list();
    }
}





